package com.future.baselib.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.EditText;

import com.future.baselib.utils.DensityUtil;

/**
 * 密码输入EditText
 * Created by jniu on 2017/9/12.
 */

public class PwdEditText extends android.support.v7.widget.AppCompatEditText {

    private final int PWD_SPACING = 5;//间隔
    private int PWD_SIZE = DensityUtil.dip2px(getContext(),24f);//密码大小
    private final int PWD_LENGTH = 6;//密码长度
    private Context mContext;
    private int mWidth;
    private int mHeight;
    private RectF mRect;//密码框
    private Paint mPwdPaint;//密码画笔
    private Paint mRectPaint;//密码框画笔
    private Paint paint;
    private int mInputLength = 0;//输入的密码长度

    private final String str = "*";

    private OnInputFinishListener onInputFinishListener;


    public PwdEditText(Context context) {
        this(context,null);
    }

    public PwdEditText(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PwdEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    private void init() {
        mPwdPaint = new TextPaint();
        mPwdPaint.setColor(Color.parseColor("#333333"));
        mPwdPaint.setStyle(Paint.Style.FILL);
        mPwdPaint.setAntiAlias(true);
        mPwdPaint.setTextAlign(Paint.Align.CENTER);
        mPwdPaint.setTextSize(PWD_SIZE);

        mRectPaint = new Paint();
        mRectPaint.setStyle(Paint.Style.FILL);
        mRectPaint.setColor(Color.parseColor("#f1f2f2"));
        mRectPaint.setAntiAlias(true);

        paint = new Paint();
        paint.setColor(Color.WHITE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mWidth = getWidth();
        mHeight = getHeight();

        canvas.drawRect(0,0,mWidth,mHeight,paint);

        int rectWidth = (mWidth - PWD_SPACING*(PWD_LENGTH-1))/PWD_LENGTH;

        for (int i = 0; i < PWD_LENGTH; i++) {
            int left = (rectWidth + PWD_SPACING)*i;
            int top = 2;
            int right = left+rectWidth;
            int bottom = mHeight - top;

            mRect = new RectF(left,top,right,bottom);
            canvas.drawRoundRect(mRect,10,10,mRectPaint);
        }

        for (int i = 0; i < mInputLength; i++) {
            int cx = rectWidth / 2 +(rectWidth + PWD_SPACING)*i;
            int cy = (int) (mHeight/2 + (mPwdPaint.descent() - mPwdPaint.ascent())/2);
            canvas.drawText(str,cx,cy,mPwdPaint);
        }
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        this.mInputLength = text.toString().length();
        invalidate();
        if (onInputFinishListener != null && mInputLength == PWD_LENGTH) {
            onInputFinishListener.onInputFinish(text.toString());
        }
    }

    public interface OnInputFinishListener{
        void onInputFinish(String password);
    }

    public void setOnInputFinishListener(OnInputFinishListener onInputFinishListener) {
        this.onInputFinishListener = onInputFinishListener;
    }
}
